home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / GIFLIB12.ARJ / GIFPOS.C < prev    next >
C/C++ Source or Header  |  1991-05-12  |  7KB  |  203 lines

  1. /*****************************************************************************
  2. *   "Gif-Lib" - Yet another gif library.                     *
  3. *                                         *
  4. * Written by:  Gershon Elber                Ver 0.1, Jul. 1989   *
  5. ******************************************************************************
  6. * Program to reposition GIF file, by changing screen size and/or image(s)    *
  7. * position. Neither image(s) not colormap(s) are changed by any mean.         *
  8. * Options:                                     *
  9. * -q : quite printing mode.                             *
  10. * -s w h : set new screen size - width & height.                 *
  11. * -i t l : set new image position - top & left (for first image).         *
  12. * -n n w h : set new image position - top & left (for image number n).         *
  13. * -h : on line help.                                 *
  14. ******************************************************************************
  15. * History:                                     *
  16. * 6 Jul 89 - Version 1.0 by Gershon Elber.                     *
  17. *****************************************************************************/
  18.  
  19. #ifdef __MSDOS__
  20. #include <stdlib.h>
  21. #include <alloc.h>
  22. #endif /* __MSDOS__ */
  23.  
  24. #include <stdio.h>
  25. #include <ctype.h>
  26. #include <string.h>
  27. #include "gif_lib.h"
  28. #include "getarg.h"
  29.  
  30. #define PROGRAM_NAME    "GifPos"
  31.  
  32. #ifdef __MSDOS__
  33. extern unsigned int
  34.     _stklen = 16384;                 /* Increase default stack size. */
  35. #endif /* __MSDOS__ */
  36.  
  37. #ifdef SYSV
  38. static char *VersionStr =
  39.         "Gif library module,\t\tGershon Elber\n\
  40.     (C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  41. static char
  42.     *CtrlStr = "GifPos q%- s%-Width|Height!d!d i%-Left|Top!d!d n%-n|Left|Top!d!d!d h%- GifFile!*s";
  43. #else
  44. static char
  45.     *VersionStr =
  46.     PROGRAM_NAME
  47.     GIF_LIB_VERSION
  48.     "    Gershon Elber,    "
  49.     __DATE__ ",   " __TIME__ "\n"
  50.     "(C) Copyright 1989 Gershon Elber, Non commercial use only.\n";
  51. static char
  52.     *CtrlStr =
  53.     PROGRAM_NAME
  54.     " q%- s%-Width|Height!d!d i%-Left|Top!d!d n%-n|Left|Top!d!d!d h%- GifFile!*s";
  55. #endif /* SYSV */
  56.  
  57. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut);
  58.  
  59. /******************************************************************************
  60. * Interpret the command line and scan the given GIF file.              *
  61. ******************************************************************************/
  62. void main(int argc, char **argv)
  63. {
  64.     int    Error, NumFiles, ExtCode, CodeSize, ImageNum = 0,
  65.     ScreenFlag = FALSE, ScreenWidth, ScreenHeight,
  66.     ImageFlag = FALSE, ImageLeft, ImageTop,
  67.     ImageNFlag = FALSE, ImageN, ImageNLeft, ImageNTop,
  68.     HelpFlag = FALSE;
  69.     GifRecordType RecordType;
  70.     GifByteType *Extension, *CodeBlock;
  71.     char **FileName = NULL;
  72.     GifFileType *GifFileIn = NULL, *GifFileOut = NULL;
  73.  
  74.     if ((Error = GAGetArgs(argc, argv, CtrlStr, &GifQuitePrint,
  75.         &ScreenFlag, &ScreenWidth, &ScreenHeight,
  76.         &ImageFlag, &ImageLeft, &ImageTop,
  77.         &ImageNFlag, &ImageN, &ImageNLeft, &ImageNTop,
  78.         &HelpFlag, &NumFiles, &FileName)) != FALSE ||
  79.         (NumFiles > 1 && !HelpFlag)) {
  80.     if (Error)
  81.         GAPrintErrMsg(Error);
  82.     else if (NumFiles > 1)
  83.         GIF_MESSAGE("Error in command line parsing - one GIF file please.");
  84.     GAPrintHowTo(CtrlStr);
  85.     exit(1);
  86.     }
  87.  
  88.     if (HelpFlag) {
  89.     fprintf(stderr, VersionStr);
  90.     GAPrintHowTo(CtrlStr);
  91.     exit(0);
  92.     }
  93.  
  94.     if (NumFiles == 1) {
  95.     if ((GifFileIn = DGifOpenFileName(*FileName)) == NULL)
  96.         QuitGifError(GifFileIn, GifFileOut);
  97.     }
  98.     else {
  99.     /* Use the stdin instead: */
  100.     if ((GifFileIn = DGifOpenFileHandle(0)) == NULL)
  101.         QuitGifError(GifFileIn, GifFileOut);
  102.     }
  103.  
  104.     /* Open stdout for the output file: */
  105.     if ((GifFileOut = EGifOpenFileHandle(1)) == NULL)
  106.     QuitGifError(GifFileIn, GifFileOut);
  107.  
  108.     /* And dump out its new possible repositioned screen information: */
  109.     if (EGifPutScreenDesc(GifFileOut,
  110.     ScreenFlag ? ScreenWidth : GifFileIn -> SWidth,
  111.     ScreenFlag ? ScreenHeight : GifFileIn -> SHeight,
  112.     GifFileIn -> SColorResolution, GifFileIn -> SBackGroundColor,
  113.     GifFileIn -> SBitsPerPixel, GifFileIn -> SColorMap) == GIF_ERROR)
  114.     QuitGifError(GifFileIn, GifFileOut);
  115.  
  116.     /* Scan the content of the GIF file and load the image(s) in: */
  117.     do {
  118.     if (DGifGetRecordType(GifFileIn, &RecordType) == GIF_ERROR)
  119.         QuitGifError(GifFileIn, GifFileOut);
  120.  
  121.     switch (RecordType) {
  122.         case IMAGE_DESC_RECORD_TYPE:
  123.         if (DGifGetImageDesc(GifFileIn) == GIF_ERROR)
  124.             QuitGifError(GifFileIn, GifFileOut);
  125.         /* Put possibly repositioned image descriptor to out file: */
  126.         if (++ImageNum == 1 && ImageFlag) {
  127.             /* First image should be repositioned: */
  128.             if (EGifPutImageDesc(GifFileOut,
  129.             ImageLeft, ImageTop,
  130.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  131.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  132.             GifFileIn -> IColorMap) == GIF_ERROR)
  133.             QuitGifError(GifFileIn, GifFileOut);
  134.         }
  135.         else if (ImageNum == ImageN && ImageNFlag) {
  136.             /* Image N should be repositioned: */
  137.             if (EGifPutImageDesc(GifFileOut,
  138.             ImageNLeft, ImageNTop,
  139.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  140.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  141.             GifFileIn -> IColorMap) == GIF_ERROR)
  142.             QuitGifError(GifFileIn, GifFileOut);
  143.         }
  144.         else {
  145.             /* No repositioning for this image: */
  146.             if (EGifPutImageDesc(GifFileOut,
  147.             GifFileIn -> ILeft, GifFileIn -> ITop,
  148.             GifFileIn -> IWidth, GifFileIn -> IHeight,
  149.             GifFileIn -> IInterlace, GifFileIn -> IBitsPerPixel,
  150.             GifFileIn -> IColorMap) == GIF_ERROR)
  151.             QuitGifError(GifFileIn, GifFileOut);
  152.         }
  153.  
  154.         /* Now read image itself in decoded form as we dont really   */
  155.         /* care what we have there, and this is much faster.         */
  156.         if (DGifGetCode(GifFileIn, &CodeSize, &CodeBlock) == GIF_ERROR ||
  157.             EGifPutCode(GifFileOut, CodeSize, CodeBlock) == GIF_ERROR)
  158.             QuitGifError(GifFileIn, GifFileOut);
  159.         while (CodeBlock != NULL) {
  160.             if (DGifGetCodeNext(GifFileIn, &CodeBlock) == GIF_ERROR ||
  161.             EGifPutCodeNext(GifFileOut, CodeBlock) == GIF_ERROR)
  162.             QuitGifError(GifFileIn, GifFileOut);
  163.         }
  164.         break;
  165.         case EXTENSION_RECORD_TYPE:
  166.         /* Skip any extension blocks in file: */
  167.         if (DGifGetExtension(GifFileIn, &ExtCode, &Extension) == GIF_ERROR)
  168.             QuitGifError(GifFileIn, GifFileOut);
  169.         if (EGifPutExtension(GifFileOut, ExtCode, Extension[0],
  170.                             Extension) == GIF_ERROR)
  171.             QuitGifError(GifFileIn, GifFileOut);
  172.  
  173.         /* No support to more than one extension blocks, so discard: */
  174.         while (Extension != NULL) {
  175.             if (DGifGetExtensionNext(GifFileIn, &Extension) == GIF_ERROR)
  176.             QuitGifError(GifFileIn, GifFileOut);
  177.         }
  178.         break;
  179.         case TERMINATE_RECORD_TYPE:
  180.         break;
  181.         default:            /* Should be traps by DGifGetRecordType. */
  182.         break;
  183.     }
  184.     }
  185.     while (RecordType != TERMINATE_RECORD_TYPE);
  186.  
  187.     if (DGifCloseFile(GifFileIn) == GIF_ERROR)
  188.     QuitGifError(GifFileIn, GifFileOut);
  189.     if (EGifCloseFile(GifFileOut) == GIF_ERROR)
  190.     QuitGifError(GifFileIn, GifFileOut);
  191. }
  192.  
  193. /******************************************************************************
  194. * Close both input and output file (if open), and exit.                  *
  195. ******************************************************************************/
  196. static void QuitGifError(GifFileType *GifFileIn, GifFileType *GifFileOut)
  197. {
  198.     PrintGifError();
  199.     if (GifFileIn != NULL) DGifCloseFile(GifFileIn);
  200.     if (GifFileOut != NULL) EGifCloseFile(GifFileOut);
  201.     exit(1);
  202. }
  203.